/*
 * @title: 通用store变量
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-03-26 19:09:35
 * @LastEditors: andy.ten@tom.com
 * @LastEditTime: 2022-04-16 10:47:24
 * @version: 1.0.4
 * @copyright: copyright (c) 2022 andyten
 *  node: you can not copy and/or use and/or modify this program free,
 *  please reserve the segment above.Please mail me if you have any question.
 */
import config from '@/store/config';
import Settings from '@/settings';
import { myLStorage } from '@/utils';
const state = {
  notifyShow: false, // 消息框是否已存在
  sidebar: { // 侧边栏 1 表示已展开 /  0 表示 已收缩
    // opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    opened: !!+myLStorage.get(Settings.sidebarStatus),
    withoutAnimation: false
  },
  device: 'desktop',
  language: 'zh',
  size: myLStorage.get(Settings.layoutSizeKey) || Settings.layoutSize
};

const mutations = {
  [config.SET_NOTIFY_STATE]: (state, isShow) => {
    state.notifyShow = isShow;
  },
  [config.SET_TOGGLE_SIDEBAR]: (state) => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened)
      myLStorage.set(Settings.sidebarStatus, 1);
    else
      myLStorage.set(Settings.sidebarStatus, 0);
  },
  [config.SET_CLOSE_SIDEBAR]: (state, withoutAnimation) => {
    myLStorage.set(Settings.sidebarStatus, 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
  [config.SET_TOGGLE_DEVICE]: (state, device) => {
    state.device = device;
  },
  [config.SET_LANGUAGE]: (state, language) => {
    state.language = language;
    myLStorage.set(Settings.languageKey, language);
  },
  [config.SET_SIZE]: (state, size) => {
    state.size = size;
    myLStorage.set(Settings.layoutSizeKey, size);
  }
};

const actions = {
  setNofifyState({ commit }, isShow) {
    commit(config.SET_NOTIFY_STATE, isShow);
  },
  setToggleSideBar({ commit }) {
    commit(config.SET_TOGGLE_SIDEBAR);
  },
  setCloseSideBar({ commit }, { withoutAnimation }) {
    commit(config.SET_CLOSE_SIDEBAR, withoutAnimation);
  },
  setToggleDevice({ commit }, device) {
    commit(config.SET_TOGGLE_DEVICE, device);
  },
  setLanguage({ commit }, language) {
    commit(config.SET_LANGUAGE, language);
  },
  setSize({ commit }, size) {
    commit(config.SET_SIZE, size);
  },
  setDict({ commit }, dict) {
    commit(config.SET_DICT, dict);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
